package com.vecharm.app.ui

import android.app.AlarmManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import com.ling.sdk.app.App
import com.tencent.bugly.Bugly
import com.tencent.bugly.beta.Beta
import com.tencent.bugly.crashreport.CrashReport
import com.vecharm.app.BuildConfig
import com.vecharm.app.utils.TOKEN


class SmartApp : App() {


    override fun onInitSdk() {
        super.onInitSdk()
        initBugly()
        Thread.setDefaultUncaughtExceptionHandler { t, e ->
            restartApp()
        }


    }

    private fun initBugly() {
        Beta.autoCheckUpgrade = false
        Beta.enableHotfix = true
        Beta.autoInit = true //自动初始化
        Beta.autoCheckUpgrade = true
        Beta.upgradeCheckPeriod = (24 * 60 * 1000).toLong()
        Beta.showInterruptedStrategy = true
        Beta.autoDownloadOnWifi = false //wifi下自动下载
        Bugly.setAppChannel(app, "SL00001")
        Bugly.init(app, BuildConfig.buglyId, BuildConfig.DEBUG)
        CrashReport.setIsDevelopmentDevice(app, BuildConfig.DEBUG)
    }


    private fun restartApp() {


        val restartIntent = PendingIntent.getActivity(this, 0, Intent(this, WelcomeActivity::class.java), PendingIntent.FLAG_ONE_SHOT)
        //退出程序
        val mgr = getSystemService(Context.ALARM_SERVICE) as AlarmManager
        //app 退出后 10s 回来
        mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 1000, restartIntent)
    }


}